﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Fu3DEngine
{
    public partial class TreeCtl : UserControl
    {
        public TreeCtl()
        {
            InitializeComponent();
        }

       
        //为了和属性窗口交互
        private PropCtl  m_prop;
        public void SetPropCtl(PropCtl p)
        {
            m_prop = p;
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

            string path = e.Node.FullPath;
            if (path.Contains(@"\"))
            {
                char[] ch=new char[]{'\\'};
                string[] nodes = path.Split(ch);
                NodeBase nb=  FuEngine.GetEngine().GetScene(nodes[0]).GetNode(nodes[1]);

                m_prop.SetObject(nb);
            }
            else
            {
                m_prop.SetObject(FuEngine.GetEngine().GetScene(path));
 
            }

           // MessageBox.Show(e.Node.FullPath);

            //if (FuEngine.GetEngine().GetScene())
            //    return;
        }

        public void InitCtl()
        {
            FuEngine fu = FuEngine.GetEngine();
            fu.SceneAdded +=new SceneEventA(fu_SceneAdded);
            fu.SceneRemoved+=new SceneEventR(fu_SceneRemoved);
        }

        private void fu_SceneAdded(SceneBase sb)
        {
            TreeNode tn = new TreeNode();
            tn.Name = sb.sid;
            tn.Text = sb.desc;


            


           

            //先使用简单的一层迭代，再日后完善了引擎之后，再继续深入写出多层结构
            if (sb.nodeDic.Count > 0)
            {
                foreach (KeyValuePair<string,NodeBase> kv in sb.nodeDic)
	            {
                    TreeNode tnc = new TreeNode();
                    tnc.Name = kv.Value .nid;
                    tnc.Text = kv.Value .desc;

                    tn.Nodes.Add(tnc);
            		 
	            }


            
               
 
            }

            treeView1.Nodes.Add(tn);


            sb.NodeAdded += new NodeEventA(this.AddNode);
            sb.NodeRemoved += new NodeEventR(this.RemoveNode);


        }

        private void fu_SceneRemoved(string sid)
        { 
        }


        protected void AddNode(NodeBase nb)
        {
            TreeNode tn = new TreeNode();
            tn.Name = nb.nid;
            tn.Text = nb.desc;

            TreeNode[] tns= treeView1.Nodes.Find(nb.SParent.sid,true);

            if (tns.Length > 0)
            {
                tns[0].Nodes.Add(tn);

            }


           
        }

        protected void RemoveNode(string sid)
        {


        }


    }

}
